// Time Tracker by Eric Nadeau (nado18@gmail.com)
// Copyright (C) 2007 by Eric Nadeau
// Distributed under the terms of the GNU General Public License (see GPL.txt)

package view;

import java.awt.*;
import java.awt.event.*;
import java.text.*;

import javax.swing.*;
import javax.swing.table.*;
import javax.swing.text.*;

import model.*;

/**
 * @author Eric Nadeau (nado18@gmail.com), on Apr 21, 2004
 */
public class MainPanel extends JPanel {
	private JButton add = new JButton("Add Project");
	private JButton remove = new JButton("Remove Project");
	private JLabel lPrice = new JLabel("", JLabel.CENTER);
	private JTable list;
	private JScrollPane spList;
	
	private Frame dialogOwner;
	private ProjectTableModel model;
	
	private GridBagLayout gbl = new GridBagLayout();
	private GridBagConstraints gbc = new GridBagConstraints();
	
	public MainPanel(Frame aDialogOwner, ProjectTableModel aModel) {
		dialogOwner = aDialogOwner;
		model = aModel;
		
		create();
		place();
		listen();
	}

	private static String formatPrice(Double val) {
		NumberFormatter nf = new NumberFormatter( new DecimalFormat("0.00") );
		try {
			return nf.valueToString(val);
		}
		catch (ParseException e) {
			return "" + val;
		}
	}
	
	private static String formatPrice(double val) {
		return formatPrice( new Double(val) );
	}
	
	private void setBill(double price) {
//		price = Math.round(price*100) / 100.0;
//		String sPrice = "";
//		sPrice = price + ((price*100%10==0) ? "0": "");
//		lPrice.setText("Bill: " + sPrice);
		
		lPrice.setText( formatPrice(price) );
	}
	
	private void create() {
		list = new JTable(model);
		list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		list.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
		list.setShowGrid(false);
		list.setRowHeight( 4 * list.getRowHeight() / 3 );
		list.setRowMargin(0);
		TableCellRenderer tcr = new TableCells();
		BooleanRenderer btc = new BooleanRenderer();
		TableCellEditor bte = new BooleanEditor( btc );
		list.setDefaultRenderer(Object.class, tcr);
		list.setDefaultRenderer(Boolean.class, btc);
		list.setDefaultEditor(Boolean.class, bte);
		list.getColumnModel().setColumnMargin(0);
		
		spList = new JScrollPane(list);
		spList.setHorizontalScrollBarPolicy( JScrollPane.HORIZONTAL_SCROLLBAR_NEVER );

		// Make the JScrollPane's background white
		spList.setOpaque(true);
		spList.setBackground(Color.WHITE);
		spList.getViewport().setOpaque(false);
	}
	
	private void listen() {
		add.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				add();
			}
		});
		
		remove.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				remove();
			}
		});
		
		list.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				if( e.getClickCount() >= 2 ) {
					edit();
				}
			}
		});
	}
	
	private void edit() {
		Project p = getSelectedProject();
		if( ProjectEditor.edit( dialogOwner, p ) ) {
			model.editedProject(p);
		}
	}

	private void add(Component child, int x, int y, int width, boolean fill) {
		gbc.gridx = x;
		gbc.gridy = y;
		gbc.gridwidth = width;
		gbc.gridheight = 1;
		gbc.weightx = 1;
		gbc.weighty = fill ? 1 : 0;
		gbc.fill = fill ? GridBagConstraints.BOTH : GridBagConstraints.HORIZONTAL;
		gbc.insets = new Insets(10, 10, 10, 10);
		
		add(child, gbc);
	}
	
	private void place() {
		setLayout(gbl);
		
		add( spList, 0, 0, 3, true );
		add( add, 0, 1, 1, false );
		add( remove, 1, 1, 1, false );
		add( lPrice, 2, 1, 1, false );
	}

	void updateColumnWidths() {
		if( list != null ) {
			TableColumnModel cm = list.getColumnModel();
			
			int margin = cm.getColumnMargin();
			int A = 48;
			int C = 96;
			int B = spList.getWidth() - (A + C + 4*margin);
			
			cm.getColumn(0).setPreferredWidth(A);
			cm.getColumn(1).setPreferredWidth(B);
			cm.getColumn(2).setPreferredWidth(C);
		}
	}
	
	private void add() {
		Project p = new Project();
		boolean ok = ProjectEditor.edit(null, p);
		if( ok ) {
			model.addProject(p);
		}
	}
	
	private void remove() {
		Project p = getSelectedProject();
		if( p != null ) {
			model.removeProject(p);
		}
	}
	
	private Project getSelectedProject() {
		int row = list.getSelectedRow();
		if( row >= 0 ) {
			return (Project)list.getValueAt(row, 1);
		}
		else {
			return null;
		}
	}
	
	public void update(double bill) {
		setBill(bill);
//		updateColumnWidths();
		super.repaint();
	}
}
